package com.etms.service.impl;

import com.etms.mapper.DeptMapper;
import com.etms.pojo.*;
import com.etms.service.*;
import com.etms.vo.Result;
import com.etms.vo.RukuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    DeptMapper deptMapper;
    @Autowired
    CangService cangService;
    @Autowired
    SupplierService supplierService;
    @Autowired
    HuopinService huopinService;
    @Autowired
    CustomService customService;
    @Override
    public Result findListById(Integer id) {//部门id 1采 2生 3销
        List<Dept>deptList=new ArrayList<>();
        if(id==1){
            deptList=deptMapper.findCaiList();
        }else if(id==2){
            deptList=deptMapper.findShengList();
        }else if(id==3){
            deptList=deptMapper.findXiaoList();
        }
        List<WareHouse>wareHouseList=cangService.findWareHouseList();
        List<Supplier>supplierList=supplierService.findSupplierList();
        List<Goods_category>goodsList=huopinService.findAll();
        List<Customer>customerList=customService.findAll();
        RukuVo rukuVo=new RukuVo();
        rukuVo.setDeptList(deptList).setSupplierList(supplierList).setWareHouseList(wareHouseList).setGoodsList(goodsList).setCustomerList(customerList);
        return Result.successed(rukuVo);
    }

    @Override
    public Dept findCaiById(Integer deptId) {
        return deptMapper.findCaiById(deptId);
    }

    @Override
    public Dept findShengById(Integer deptId) {
        return deptMapper.findShengById(deptId);
    }

    @Override
    public Dept findXiaoById(Integer deptId) {
        return deptMapper.findXiaoById(deptId);
    }

    @Override
    public Result findItemListById(Integer id) {
        List<DeptItem> itemList=null;
        if(id==1){//1采2生3销
            itemList=deptMapper.findCaiItemList();
        }
        if (id==2){
            itemList=deptMapper.findShengItemList();
        }
        if (id==3){
            itemList=deptMapper.findXiaoItemList();
        }
        return Result.successed(itemList);
    }

    @Override
    public Result findItemByDaidAndXid(Integer daid, Integer xid) {
        DeptItem deptItem=null;
        System.out.println("xid-->"+xid);
        if (daid==1){
            deptItem=deptMapper.findCaiItemById(xid);
        }
        if (daid==2){
            deptItem=deptMapper.findShengItemById(xid);
        }
        if (daid==3){
            deptItem=deptMapper.findXiaoItemById(xid);
        }
        return Result.successed(deptItem);
    }

    @Override
    public Result updateOrAdd(DeptItem deptItem) {
        Integer daid = deptItem.getDaid();
        Integer id = deptItem.getId();
        if(id!=null&&id!=0) {//走更新
            if(daid==1){
                deptMapper.updateCai(deptItem);
            }
            if(daid==2){
                deptMapper.updateSheng(deptItem);
            }
            if(daid==3){
                deptMapper.updateXiao(deptItem);
            }
            return Result.successed("修改成功!",null);
        }

        if(daid==1){
            deptMapper.addCai(deptItem);
        }
        if(daid==2){
            deptMapper.addSheng(deptItem);
        }
        if(daid==3){
            deptMapper.addXiao(deptItem);
        }
        return Result.successed("添加成功!",null);
    }

    @Override
    public Result delItemById(Integer daId, Integer xid) {
        if(daId==1){
            deptMapper.delCai(xid);
        }
        if(daId==2){
            deptMapper.delSheng(xid);
        }
        if(daId==3){
            deptMapper.delXiao(xid);
        }
        return Result.successed("删除成功!",null);
    }
}
